//
//	xy
//
#include "WebClientEngine.h"
#include "RocketPipe.h"
#include "RocketRequest.h"
#include "RocketResponse.h"
#include "PortocolLogin.h"

CRocketPipe* CRocketPipe::NewL(CEngine* pEngine)
	{
		CRocketPipe* self = CRocketPipe::NewLC(pEngine);
		CleanupStack::Pop();
		return self;
	}

CRocketPipe* CRocketPipe::NewLC(CEngine* pEngine)
	{
		CRocketPipe* self = new (ELeave) CRocketPipe(pEngine);
		CleanupStack::PushL(self);
		self->ConstructL();
		return self;
	}

CRocketPipe::~CRocketPipe()
{
	//delete iXmlHandler;
	//iXmlHandler = NULL;
	iBuffer.Close();	
}

void CRocketPipe::DoCancel()
{
	iTimer.Cancel();
}
_LIT8(KContentType, "text/xml");

void CRocketPipe::RunL()
{
	iTimer.Cancel();
	if(KErrNone == iStatus.Int() 
			&& EPipeStatusNone == iPipeStatus)
	{
		CRocketRequest* aRequest = iArray.At(0);
		iPipeStatus = EPipeStatusRequest;
		//iHttpEngine->IssueHTTPGetL(aRequest->GetURLPtr());
		iHttpEngine->IssueHTTPPostL(aRequest->GetURLPtr(),KContentType, aRequest->GetDataPtr());
	}
	else
		iPipeStatus = EPipeStatusNone;
}

TBool CRocketPipe::AppendRequest(CRocketRequest* aRequest)
	{
		iArray.AppendL(aRequest);
		if(EPipeStatusNone == iPipeStatus)
		{
			iTimer.After(iStatus, 1000);
			SetActive();
		}
		return ETrue;
	}

void CRocketPipe::AppendDataL(const TDesC8& data)
{
	int newLength = iBuffer.Length() + data.Length();
	if (iBuffer.MaxLength() < newLength) {
		iBuffer.ReAllocL(newLength);
	}
	iBuffer.Append(data);
}

CRocketPipe::CRocketPipe(CEngine* aEngine) : CActive(EPriorityStandard), iArray(20)
{
	iEngine = aEngine;
	CActiveScheduler::Add(this);
}

void CRocketPipe::ConstructL()
{
	//iXmlHandler =  CXmlHandler::NewL();
	iTimer.CreateLocal();
	iPipeStatus = EPipeStatusNone;
	
	iHttpEngine = CWebClientEngine::NewL(*this);
	iHttpEngine->OpenSessionL();
}
	
void CRocketPipe::ClientOpenSessionFailedL(CWebClientEngine& anEngine, 
											  TInt anError)
{
	iPipeStatus = EPipeStatusError;
}
	
void CRocketPipe::ClientConnectingL(CWebClientEngine& anEngine)
{
	
}
	
void CRocketPipe::ClientHeaderReceivedL(CWebClientEngine& anEngine, 
										   const TDesC& aHeaderData)
{
	iPipeStatus = EPipeStatusResponse;
}

void CRocketPipe::ClientBodyReceivedL(CWebClientEngine& /*anEngine*/, 
										 const TDesC8& aBodyData)
{
	iPipeStatus = EPipeStatusResponse;
	AppendDataL(aBodyData);
}

void CRocketPipe::ClientConnectionCanceledL(CWebClientEngine& anEngine)
{
	iPipeStatus = EPipeStatusError;
}
		
void CRocketPipe::ClientResponseCompleteL(CWebClientEngine& anEngine)
{

}

_LIT8(KNumber, "interfaceno ");
CRocketResponse* BuildResponse(RBuf8& aBuffer)
{
	//iBuffer = aBuffer.Alloc();
	
	TInt nPos = aBuffer.Find(KNumber());
	TPtrC8 ptr(aBuffer.Mid(nPos + KNumber().Length()));
	nPos = ptr.Find(_L8("\""));
	TPtrC8 ptr2 = ptr.Mid(nPos + 1);
	nPos = ptr2.Find(_L8("\""));
	
	TBuf8<20> bufNumber;
	bufNumber.Zero();
	bufNumber.Append(ptr2.Left(nPos));
	
	TLex8 alex(bufNumber);
	TInt aNumber;
	alex.Val(aNumber);
	
	CRocketResponse* aResponse = NULL;
	switch(aNumber)
	{
	case 1:
		aResponse = new(ELeave)CRocketResponseLogin();
	case 2:
	default:
		break;
	}
	return aResponse;
}

void CRocketPipe::ClientTransactionSucceededL(CWebClientEngine& anEngine)
{
	if(iBuffer.Length() > 0)
			//&& iXmlHandler->ParseL(iBuffer))
	{
		CRocketResponse* aResp = BuildResponse(iBuffer);
		aResp->Decode(iBuffer);
		CRocketRequest* aRequest = iArray.At(0);
		aRequest->iObserver->ProcessNetworkEvent(aRequest,aResp);
		iArray.Delete(0);
		delete aRequest;
		delete aResp;
	}
	iBuffer.SetLength(0);
	iBuffer.ReAlloc(0);
	
	iTimer.Cancel();
	iPipeStatus = EPipeStatusNone; 
	iTimer.After(iStatus, 1000);
	SetActive();
}

void CRocketPipe::ClientTransactionFailedL(CWebClientEngine& anEngine)
{
	iPipeStatus = EPipeStatusError;
}

void CRocketPipe::ClientUnknownEventL(CWebClientEngine& anEngine, 
		 								  TInt aStatus)
{

}
		
void CRocketPipe::ClientRunErrorL(CWebClientEngine& anEngine , TInt anError)
{
	iPipeStatus = EPipeStatusError;
}
		
TBool CRocketPipe::ClientGetCredentialsL(CWebClientEngine& anEngine, 
											const TUriC8& aUri, 
											const TDesC8& aRealm, 
											TDes& aUsername, 
											TDes& aPassword)
{
	return EFalse;
}

